<template>
  <div>
    <div class="imgList">
      <div v-if="imageUrl.length">
        <div class="img_list" v-for="(item, index) in imageUrl" :key="index">
          <img :src="item" alt="图片加载失败" />
          <div class="remove">
            <span @click="viewImg(item)">
              <i class="el-icon-view"></i>
            </span>
            <span @click="removeImg(index)">
              <i class="el-icon-delete"></i>
            </span>
          </div>
        </div>
        <el-dialog title="图片预览" :visible.sync="viewImgX" width="30%">
          <img :src="img" alt style="width:100%;height:100%" />
        </el-dialog>
      </div>
      <el-upload
        v-show="showUpload"
        class="avatar-uploader"
        action
        :show-file-list="false"
        accept="image/jpeg, image/jpg, image/png"
        :auto-upload="false"
        :on-change="uploadMap"
      >
        <i class="el-icon-plus"></i>
      </el-upload>
    </div>
  </div>
</template>

<script>
import { uploadImg } from "../../api/public/api";
export default {
  data() {
    return {
      imageUrl: [],
      img: "",
      showUpload: true,
      viewImgX: false,
    };
  },
  methods: {
    viewImg(i) {
      this.img = i;
      this.viewImgX = true;
    },
    removeImg(index) {
      this.imageUrl.splice(index, 1);
      if (this.imageUrl.length === 1) {
        this.showUpload = false;
      } else {
        this.showUpload = true;
      }
    },
    uploadMap(file) {
      if (file) {
        // let formData = new FormData();
        // formData.append("source", file.raw);
        // uploadImg(formData).then((res) => {
        //   this.imageUrl.push(res.data.url);
        //   if (this.imageUrl.length === 1) {
        //     this.showUpload = false;
        //   } else {
        //     this.showUpload = true;
        //   }
        // });
      }
    },
  },
};
</script>
<style  scope>
.avatar-uploader .el-upload > i {
  font-size: 22px;
  color: #ccc;
}
.avatar-uploader {
  width: 200px;
  height: 200px;
  text-align: center;
  line-height: 200px;
  border: 1px dashed #ccc;
}
.avatar-uploader .el-upload {
  width: 200px;
  height: 200px;
}
.img_list {
  position: relative;
}
.img_list > img {
  width: 200px;
  height: 200px;
}
.remove > span {
  margin-right: 20px;
  margin-left: 20px;
  cursor: pointer;
}
.imgList .img_list .remove {
  font-size: 30px;
  color: #fff;
  line-height: 200px;
  text-align: center;
  width: 200px;
  height: 200px;
  display: none;
  position: absolute;
  top: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.5);
}
.imgList .img_list:hover .remove {
  display: block;
}
</style>